<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>自定义遍历数据</title>
</head>

<body>

  <script>
    const banji = {
      name: '终极一班',
      stus: [
        'xiaoming',
        'xiaoning',
        'xiaotian',
        'knight'
      ],
      [Symbol.iterator]() {
        // 定义索引
        let index = 0;

        const _this = this;
        return {
          next: function () {
            if (index < _this.stus.length) {
              const result = { value: _this.stus[index], done: false }
              index++;
              return result;
            } else {
              return { value: undefined, done: true }
            }
          }
        }
      }
    }

    // 遍历 banji，得到 banji.stus 中的值
    for (v of banji) {
      console.log(v)
    }
  </script>
</body>

</html>